#!/usr/bin/Rscript --vanilla
# this script creates a list of quantified data.frames of all or one particular sd/median group.
# the script uses as raw data the cleanStockData RData file
# it outputs the calculated list of data.frame as RData file

this.version <- "v1_1"

## global parameters follow
ADX.n <- 14
ADX.threshold <- c(15,25)
ADX.maType <- "EMA"

suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(quantmod))
suppressPackageStartupMessages(library(cssupport))
suppressPackageStartupMessages(library(quantify))

outputDir <- "/home/voellenk/Dropbox/constellation"

option_list <- list(
  make_option(c("--group"), default="all",
              help="id of group to process [default \"%default\"]\n
                    Example: --group=1_2 (sdG_medianG)"),
  make_option(c("--number"), type="integer", default=20,
              help="number of random symbols in one group to process [default %default]",
              metavar="number"),
  make_option(c("--seed"), type="integer", default=123,
              help="seed for random number generation [default %default]",
              metavar="number"),
  make_option(c("--future_periods"), type="integer", default=5,
              help="number of periods to calculate future performance [default %default]",
              metavar="number"),
  make_option(c("--method"), default="o1",
              help="price to take for position entry [default \"%default\"]")
)

opt <- parse_args(OptionParser(option_list=option_list))
args <- commandArgs(trailingOnly=TRUE)
# the options are accessible in the script as
# opt$group
# for debug: 
# opt <- list("group"="1_2", "number"=4, "seed"=123, "future_periods"=5, "method"="o1")

set.seed(opt$seed)

# define helper functions for this script
get2 <- function(var) {
  get(var, envir=TSDATA)
}

# load financial data 2000-2013 into own environment
TSDATA <- new.env()
load("/home/voellenk/stratlab_workdir/cleanData/cleanStockData_2000_2014_quantiles.RData", envir=TSDATA)
assign("syms", get("syms", envir=TSDATA)) # copy syms data.frame into global environment

# create sample data.frame (random items of each group)
# sd.q median.q  x.1  x.2 x.3  x.4
# 1    2         ACGL DCI XRAY MHFI
if (opt$group == "all") {
  sample <- aggregate(syms$Symbol, by=list(sd.q=syms$sd.q, median.q=syms$median.q), FUN=sample, size=opt$number)
} else {
  gID <- strsplit(opt$group, "_", fixed=TRUE)
  if (length(gID[[1]]) != 2) {
    stop("invalid group parameter given")
  } else {
    gID.sd <- as.numeric(gID[[1]][1])
    gID.median <- as.numeric(gID[[1]][2])
  }
  sample <- aggregate(syms$Symbol, by=list(sd.q=syms$sd.q, median.q=syms$median.q), FUN=sample, size=opt$number)
  sample <- sample[sample$sd.q==gID.sd &sample$median.q==gID.median, ]
}

print("working with sample:")
sample

DF.list <- list()
# loop through every line of sample data.frame (every group)
for (i in 1:nrow(sample)) {
  this.group <- paste("G", sample[i,"sd.q"], sample[i,"median.q"], sep="_")
  DF.list[[this.group]] <- list()
  print(paste("... doing group", this.group))
  # loop through every symbol of a group
  for (j in 1:length(sample[i,3])) {
    this.sym <- sample[i,3][j]
    print(paste("... ... symbol:", this.sym))    
    # calculating quantifications of time series
    this.TS <- get2(this.sym)[,1:4]  # only include OHLC columns. we are starting with 4 columns
    ### we add an ADX status
    this.TS <- cbind(this.TS, qADXStatus(this.TS[,1:4], n=ADX.n, threshold=ADX.threshold, maType=ADX.maType))
    
    # adding performance columns
    this.TS <- cbind(this.TS, qFindOpportunity(this.TS[,1:4], n=opt$future_periods, method=opt$method))
    CC <- this.TS[complete.cases(this.TS),]
    if (nrow(CC) > 1000) {
      CCC <- CC[200:nrow(CC),c(5:ncol(CC))]   # omit first observations where quantile status is not swung in
      DF.list[[this.group]][[this.sym]] <- as.data.frame(CCC, row.names=NULL)      
    } else {
      print(paste0("... ... ... error with symbol ", this.sym, ". Less than 1000 complete cases."))
    }
  }
}

# store collected data.frames in Rdata variable
optvec<-unlist(opt)
for(i in 1:length(optvec)) {
  optvec[i] <- paste(names(optvec[i]), optvec[i], sep="=")
}
filename <- sprintf("%s/%s.Rdata", outputDir, paste(this.version, paste(optvec, collapse="_"), sep="_"))

print(paste("saving file to", filename))
save(DF.list, file=filename)